package com.tr.rentcar.controller.houtai.statistics;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.model.bean.CarBrand;
import com.tr.rentcar.model.dao.CarBrandDao;
import com.tr.rentcar.model.dao.OrderDao;
import com.tr.rentcar.model.dao.UserDao;

/**
 * Servlet
 */
@WebServlet("/online")
public class OnlineTjController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getRequestDispatcher("/WEB-INF/jsp/houtai_online.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		UserDao ud = new UserDao();
		Map map = new HashMap();
		Calendar cal = Calendar.getInstance();
		int now = cal.get(Calendar.YEAR);
		String data = req.getParameter("data");
		String tim = req.getParameter("time");
		String t[] = tim.split("-");
		int year = Integer.parseInt(t[0]);// 获取年份
		int month = Integer.parseInt(t[1]); // 获取月份
		int day = Integer.parseInt(t[2]); // 获取日
		// 获取最大天数
		String lastDay = getLastDayOfMonth(year, month);
		String last[] = lastDay.split("-");
		int n = Integer.parseInt(last[2]);
		long num;
		// 按天统计
		if (data.equals("day")) {
			map.clear();
			for (int i = 1; i <= n; i++) {
				String time = year + "-" + String.format("%02d", month) + "-" + String.format("%02d", i);
				try {
					num = ud.getPeopleCount(time);
					map.put(String.valueOf(i), num);

				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			out.print(JSONArray.toJSONString(map));
		}
		// 按月统计
		if (data.equals("month")) {
			map.clear();
			for (int i = 1; i <= 12; i++) {
				String time = year + "-" + String.format("%02d", i);
				try {
					num = ud.getPeopleCount(time);
					map.put(String.valueOf(i), num);

				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			// System.out.println(JSONArray.toJSONString(map));
			out.print(JSONArray.toJSONString(map));
		}

		// 按年统计
		if (data.equals("year")) {
			map.clear();
			for (int i = 2016; i <= now; i++) {
				String time = i + "";
				try {
					num = ud.getPeopleCount(time);
					map.put(String.valueOf(i), num);

				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			// System.out.println(JSONArray.toJSONString(map));
			out.print(JSONArray.toJSONString(map));
		}
	}

	// 获取最大天数
	public String getLastDayOfMonth(int year2, int month2) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year2);
		// 设置月份
		cal.set(Calendar.MONTH, month2 - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String lastDayOfMonth = sdf.format(cal.getTime());

		return lastDayOfMonth;
	}

}
